#include <QApplication>
#include "widgetone.h"
#include <widgettwo.h>
#include <QPushButton>
#include <QHBoxLayout>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QDebug>
#include <QLabel>
#include <QMessageBox>
#include "bandsolver.h"
#include <QElapsedTimer>
#include <QTime>
#include <QWindow>

class Button3 : public QPushButton {
public:
    Button3() : QPushButton() {
        setAcceptDrops(true);
    }

protected:
    void dragEnterEvent(QDragEnterEvent *event) override {
        if (event->mimeData()->hasUrls()) {
            for (auto &url :event->mimeData()->urls()) {
                if (url.isLocalFile() && url.toString().endsWith(".dat")) {
                    event->acceptProposedAction();
                    return;
                }
            }
        }
    }

    void dropEvent(QDropEvent *e) override {
        auto urls = e->mimeData()->urls();
        for (auto &url:urls) {
            if (url.isLocalFile() && url.toLocalFile().endsWith(".dat")) {
                auto fileName = url.toLocalFile();

                auto *msgBox = new QMessageBox(this);
                msgBox->setWindowTitle(fileName);
                msgBox->addButton("OK", QMessageBox::RejectRole);
                msgBox->setAttribute(Qt::WA_DeleteOnClose);
                msgBox->setModal(false);
                if (m_solver.loadFromFile(fileName.toLocal8Bit().toStdString())) {
                    QElapsedTimer timer;
                    timer.start();
                    m_solver.solve();
                    msgBox->setIcon(QMessageBox::Information);
                    msgBox->setText(QString("计算成功，耗时%1").arg(QTime(0, 0, 0, timer.elapsed()).toString("H小时m分s.z秒")));
                    msgBox->setDetailedText(
                            QString("前40个解为（如果有这么多的话）：\n%1").arg(m_solver.ansToString(40).c_str()));

                } else {

                    msgBox->setIcon(QMessageBox::Critical);
                    msgBox->setText("无法通过校验，请检查文件是否损坏!");
                    msgBox->setDetailedText(QString("无法打开文件\"%1\",请检查程序是否由权限读取或者"
                                                    "该文件内受否包含有效的线性方程组").arg(fileName));

                }
                msgBox->show();
            }
        }
    }

private:
    BandSolver m_solver;

};

int main(int argc, char *argv[]) {

    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication::setApplicationName("计算方法上机作业(作者：王志华；邮箱:1069155322@qq.com)");
    QApplication::setApplicationVersion("1.0.0");
    QApplication::setFont(QFont("Microsoft Yahei"));

//    QWindow::setIcon(QIcon("C:/Users/Zhihua/Desktop/computational-methods/mylogo.jpg"));

    QApplication app(argc, argv);
//    QApplication::setWindowIcon(QIcon("C:/Users/Zhihua/Desktop/computational-methods/mylogo.jpg"));
    WidgetOne w1;
    WidgetTwo w2;
    QWidget w;
    w.resize(640, 480);
    QPushButton btn1, btn2;
    Button3 btn3;
    btn1.setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding));
    btn2.setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding));
    btn3.setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding));
    btn1.setText("第一题");
    btn2.setText("第二题");
    btn3.setText("第三题\n(直接把\n待求解\n方程组\n对应的\n文件\n往上拽！！！)");
    QHBoxLayout layout;
    layout.addWidget(&btn1);
    layout.addWidget(&btn2);
    layout.addWidget(&btn3);
    w.setLayout(&layout);
    QObject::connect(&btn1, &QPushButton::clicked, &w1, &QWidget::show);
    QObject::connect(&btn2, &QPushButton::clicked, &w2, &QWidget::show);
    QObject::connect(&btn3, &QPushButton::clicked, [&]() {
        QMessageBox::aboutQt(nullptr);
    });
    w.show();
    return app.exec();
}
